#include "FileGrid.h"
#include "wx/stdpaths.h"
#include "wx/filename.h"

FileGrid::FileGrid(wxWindow *parent, wxString filename)
        : wxGrid(parent, wxID_ANY)
{
  // Get the path for the load files
  wxStandardPathsBase& stdp = wxStandardPaths::Get();
  wxFileName fname;

  //fname.AssignDir(stdp.GetDocumentsDir());
  fname.AssignDir(wxGetCwd());
  fname.RemoveLastDir();  // remove the 'bin'

  //fname.AppendDir(wxT(PROJECT_FOLDER));
  fname.AppendDir(wxT(PROJECT_EXAMPLES_FOLDER)); // add the 'Examples' path to the full path.
  fname.Assign(fname.GetFullPath(), filename);

  //wxPrintf(_T("File dir:\t\t%s\n"), fname.GetFullPath().c_str());
  // Open up the file
  m_pFilename = fname.GetFullPath();
  m_pTextFile  = new wxTextFile(m_pFilename);
  m_pTextFile->Open(m_pFilename);

  CreateGrid(0, 0);

  // Get header line
  wxString m_pHeader = m_pTextFile->GetFirstLine();

  // Split up header string to be the column headers in the grid
  int col = 0;
  wxStringTokenizer tkz(m_pHeader, wxT(FILE_DELIMITER));
  while(tkz.HasMoreTokens())
  {
    AppendCols(1);
    wxString token = tkz.GetNextToken();
    this->SetColLabelValue(col, token);
    col++;
  }

  // Get grid contents
  wxString line;
  int row = 0;
  for(line = m_pTextFile->GetNextLine();
      !m_pTextFile->Eof();
      line = m_pTextFile->GetNextLine())
  {
    AppendRows(1);
    int col = 0;
    wxStringTokenizer tkz(line, wxT(FILE_DELIMITER));
    while(tkz.HasMoreTokens())
    {
      wxString token = tkz.GetNextToken();
      SetCellValue(row, col, token);
      col++;
    }
    row++;
  }
  m_pTextFile->Close();
  AutoSizeColumns();
}

FileGrid::~FileGrid()
{
}

BEGIN_EVENT_TABLE ( FileGrid, wxGrid )

END_EVENT_TABLE()
